const moment = require('../libs/moment')

class DateUtils {
  getAge(birthday, time = new moment()) {
    if ((typeof birthday) === "string") {
      birthday = new moment(birthday, "YYYY-MM-DD")
    } else if (moment.isDate(birthday)) {
      birthday = moment(birthday)
    }
    if ((typeof time) === "string") {
      time = new moment(time, "YYYY-MM-DD HH:mm:ss")
    } else if (moment.isDate(time)) {
      time = moment(time)
    }
    let birth = [birthday.year(), birthday.month(), birthday.date()]
    let curTime = [time.year(), time.month(), time.date()]

    return moment(curTime).diff(birth, "years")
  }
}

module.exports = new DateUtils()